/*
*VMukti -- An open source video conferencing platform.
*
* Copyright (C) 2007 - 2008, VMukti Solutions Pvt. Ltd.
*
* Hardik Sanghvi <hardik@vmukti.com>
*
* See http://www.vmukti.com for more information about
* the VMukti project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
using VMukti.DataAccess.VMuktiGrid;
using System.Data.SqlClient;
using System.Collections.Generic;
using VMukti.Business.CommonDataContracts;
using VMuktiAPI;
using System;


namespace VMukti.Business.VMuktiGrid
{
    public class ClsModuleCollection : ClsBaseCollection<ClsModule>
    {
        public static ClsModuleCollection GetAll()
        {
            try
            {
                ClsModuleCollection obj = new ClsModuleCollection();
                if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithHttp)
                {
                    try
                    {
                        obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module order by ModuleName").dsInfo);
                    }
                    catch (System.ServiceModel.EndpointNotFoundException e)
                    {
                        e.Data.Add("My Key", "GetAll()--:--ClsModuleCollection.cs--:--" + e.Message + " :--:--");
                        ClsException.LogError(e);
                        ClsException.WriteToErrorLogFile(e);
                        VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                        obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module order by ModuleName").dsInfo);
                    }
                    catch (System.ServiceModel.CommunicationException e)
                    {
                        e.Data.Add("My Key", "GetAll()--:--ClsModuleCollection.cs--:--" + e.Message + " :--:--");
                        ClsException.LogError(e);
                        ClsException.WriteToErrorLogFile(e);
                        VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                        obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module order by ModuleName").dsInfo);
                    }

                }
                else
                {
                    obj.MapObjects(new ClsModuleDataService().GetModules());
                }
                return obj;
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "VMukti--:--VmuktiBussiness--:--VmuktiGrid--:--Module--:--ClsModuleCollection.cs--:--ClsModuleCollection()--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
                return null;
            }
        }

        public static ClsModuleCollection GetNCMod()
        {
            try
            {

                ClsModuleCollection obj = new ClsModuleCollection();
                if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithHttp)
                {
                    try
                    {
                        obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module where NeedsAuthentication='true'and IsCollaborative='false' order by ModuleName").dsInfo);
                    }
                    catch (System.ServiceModel.EndpointNotFoundException e)
                    {
                        e.Data.Add("My Key", "GetNCMod()--:--ClsModuleCollection.cs--:--" + e.Message + " :--:--");
                        ClsException.LogError(e);
                        ClsException.WriteToErrorLogFile(e);
                        VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                        obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module where NeedsAuthentication='true'and IsCollaborative='false' order by ModuleName").dsInfo);
                    }
                    catch (System.ServiceModel.CommunicationException e)
                    {
                        e.Data.Add("My Key", "GetNCMod()--:--ClsModuleCollection.cs--:--" + e.Message + " :--:--");
                        ClsException.LogError(e);
                        ClsException.WriteToErrorLogFile(e);
                        VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                        obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module where NeedsAuthentication='true'and IsCollaborative='false' order by ModuleName").dsInfo);
                    }

                }
                else
                {
                    obj.MapObjects(new ClsModuleDataService().GetNonCollaborativeModules());
                }
                return obj;
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "VMukti--:--VmuktiBussiness--:--VmuktiGrid--:--Module--:--ClsModuleCollection.cs--:--ClsModuleCollection()--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
                return null;
            }
        }

        public static ClsModuleCollection GetNonAuthenticatedMod()
        {
            try
            {

                ClsModuleCollection obj = new ClsModuleCollection();
                if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithHttp)
                {

                    try
                    {
                        obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module where NeedsAuthentication='false' order by ModuleName").dsInfo);
                    }
                    catch (System.ServiceModel.EndpointNotFoundException e)
                    {
                        e.Data.Add("My Key", "GetNonAuthenticatedMod)--:--ClsModuleCollection.cs--:--" + e.Message + " :--:--");
                        ClsException.LogError(e);
                        ClsException.WriteToErrorLogFile(e);
                        VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                        obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module where NeedsAuthentication='false' order by ModuleName").dsInfo);
                    }
                    catch (System.ServiceModel.CommunicationException e)
                    {
                        e.Data.Add("My Key", "GetNonAuthenticatedMod--:--ClsModuleCollection.cs--:--" + e.Message + " :--:--");
                        ClsException.LogError(e);
                        ClsException.WriteToErrorLogFile(e);
                        VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                        obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module where NeedsAuthentication='false' order by ModuleName").dsInfo);
                    }

                    //List<clsSqlParametersInfo> lstSP = new List<clsSqlParametersInfo>();

                    //clsSqlParametersInfo objInfo = new clsSqlParametersInfo();
                    //objInfo.Direction = "Input";
                    //objInfo.PName = "@pRoleId";
                    //objInfo.PValue = 1;                
                    //objInfo.PDBType = "BigInt";
                    //objInfo.PSize = 200;
                    //lstSP.Add(objInfo);

                    ////clsSqlParametersInfo objInfo1 = new clsSqlParametersInfo();
                    ////objInfo1.Direction = "Output";
                    ////objInfo1.PName = "@Result";
                    ////objInfo1.PDBType = "Bit";
                    ////objInfo1.PSize = 1;
                    ////lstSP.Add(objInfo1);

                    ////SqlParameter sp = new SqlParameter();

                    ////sp.Direction = System.Data.ParameterDirection.Input;
                    ////sp.ParameterName = "@pModuleName";

                    ////sp.Value="Chat";
                    ////sp.SqlDbType = System.Data.SqlDbType.NVarChar;
                    ////lstSP.Add(sp);

                    ////SqlParameter sp1 = new SqlParameter();
                    ////sp1.Direction = System.Data.ParameterDirection.Output;
                    ////sp1.ParameterName = "@Result";
                    ////sp1.SqlDbType = System.Data.SqlDbType.Bit;
                    ////lstSP.Add(sp1);

                    //clsSqlParameterContract CSqlInfo = new clsSqlParameterContract();
                    //CSqlInfo.objParam = lstSP;

                    //VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("spGCModules", CSqlInfo);
                    //VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module where NeedsAuthentication='false' order by ModuleName");
                }
                else
                {
                    obj.MapObjects(new ClsModuleDataService().GetNonAuthenticatedModules());
                }
                return obj;
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "VMukti--:--VmuktiBussiness--:--VmuktiGrid--:--Module--:--ClsModuleCollection.cs--:--ClsModuleCollection()--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
                return null;
            }
        }

        public static ClsModuleCollection GetCMod(int intRoleId)
        {
            try
            {

                ClsModuleCollection obj = new ClsModuleCollection();
                if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithHttp)
                {
                    List<clsSqlParametersInfo> lstSP = new List<clsSqlParametersInfo>();

                    clsSqlParametersInfo objInfo = new clsSqlParametersInfo();
                    objInfo.Direction = "Input";
                    objInfo.PName = "@pRoleId";
                    objInfo.PValue = intRoleId;
                    objInfo.PDBType = "BigInt";
                    objInfo.PSize = 200;
                    lstSP.Add(objInfo);

                    clsSqlParameterContract CSqlInfo = new clsSqlParameterContract();
                    CSqlInfo.objParam = lstSP;

                    try
                    {
                        obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("spGCModules", CSqlInfo).dsInfo);
                    }
                    catch (System.ServiceModel.EndpointNotFoundException e)
                    {
                        e.Data.Add("My Key", "GetCMod(int intRoleId)--:--ClsModuleCollection.cs--:--" + e.Message + " :--:--");
                        ClsException.LogError(e);
                        ClsException.WriteToErrorLogFile(e);
                        VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                        obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("spGCModules", CSqlInfo).dsInfo);
                    }
                    catch (System.ServiceModel.CommunicationException e)
                    {
                        e.Data.Add("My Key", "GetCMod(int intRoleId)--:--ClsModuleCollection.cs--:--" + e.Message + " :--:--");
                        ClsException.LogError(e);
                        ClsException.WriteToErrorLogFile(e);
                        VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                        obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("spGCModules", CSqlInfo).dsInfo);
                    }


                }
                else
                {
                    obj.MapObjects(new ClsModuleDataService().GetCollaborativeModules(intRoleId));
                }
                return obj;
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "VMukti--:--VmuktiBussiness--:--VmuktiGrid--:--Module--:--ClsModuleCollection.cs--:--GetCMod()--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
                return null;
            }
        }
        public static ClsModuleCollection GetOnlyCollMod()
        {
            try
            {

                ClsModuleCollection obj = new ClsModuleCollection();
                if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithHttp)
                {
                    try
                    {
                        obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module where IsCollaborative='true' and NeedsAuthentication='true' order by ModuleName").dsInfo);
                    }
                    catch (System.ServiceModel.EndpointNotFoundException e)
                    {
                        e.Data.Add("My Key", "GetOnlyCollMod(int intRoleId)--:--ClsModuleCollection.cs--:--" + e.Message + " :--:--");
                        ClsException.LogError(e);
                        ClsException.WriteToErrorLogFile(e);
                        VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                        obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module where IsCollaborative='true' and NeedsAuthentication='true' order by ModuleName").dsInfo);
                    }
                    catch (System.ServiceModel.CommunicationException e)
                    {
                        e.Data.Add("My Key", "GetOnlyCollMod(int intRoleId)--:--ClsModuleCollection.cs--:--" + e.Message + " :--:--");
                        ClsException.LogError(e);
                        ClsException.WriteToErrorLogFile(e);
                        VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                        obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module where IsCollaborative='true' and NeedsAuthentication='true' order by ModuleName").dsInfo);
                    }

                }
                else
                {
                    obj.MapObjects(new ClsModuleDataService().GetOnlyCollaborativeModules());
                }
                return obj;
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "VMukti--:--VmuktiBussiness--:--VmuktiGrid--:--Module--:--ClsModuleCollection.cs--:--GetOnlyCollMod()--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
                return null;
            }
        }
    }
}
